/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * onefactorizations
 * Copyright (C)  2010 <>
 * 
 * onefactorizations is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * onefactorizations is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "clear-iterator.h"

ClearIterator::ClearIterator(bedge_iterator edge_iter, bedge_iterator end, int remove1, int remove2, const BoostGraph* g) 
: vremove1(remove1), vremove2(remove2), end_iter(end), iter(edge_iter), graph(g)
{
	// Want to make sure starting position is valid so first dereference is one of the desired edges
	if (iter != end_iter && (source(*iter,*graph) == vremove1 || source(*iter,*graph) == vremove2 || 
	  target(*iter,*graph) == vremove1 || target(*iter,*graph) == vremove2))
		this->operator++();
}

ClearIterator::ClearIterator(ClearIterator const &other)
: vremove1(other.vremove1), vremove2(other.vremove2), iter(other.iter), end_iter(other.end_iter), graph(other.graph)
{
	// Want to make sure starting position is valid so first dereference is one of the desired edges
	if (iter != end_iter && (source(*iter,*graph) == vremove1 || source(*iter,*graph) == vremove2 || 
	  target(*iter,*graph) == vremove1 || target(*iter,*graph) == vremove2))
		this->operator++();
}

ClearIterator::~ClearIterator() {}

std::pair<int,int> ClearIterator::operator*()
{
	// Not supposed to dereference end - should an exception be thrown here?
	return std::pair<int,int>(source(*iter,*graph),target(*iter,*graph));
}

ClearIterator& ClearIterator::operator++()
{
	if (iter != end_iter)
	{
		iter++;
		// Want to increment current location until we reach an edge without vremove 1 and vremove2, or until the end is hit
		while (iter != end_iter && (source(*iter,*graph) == vremove1 || source(*iter,*graph) == vremove2 || 
		 target(*iter,*graph) == vremove1 || target(*iter,*graph) == vremove2))
			iter++;
	}
	return *this;
}

ClearIterator ClearIterator::operator++(int)
{
	ClearIterator copy(*this);
	// Same as prefix increment except want to return copy of this before increment was performed
	this->operator++();
	return copy;
}

bool ClearIterator::operator==(const ClearIterator& other)
{
	return (iter == other.iter);
}

bool ClearIterator::operator!=(const ClearIterator& other)
{
	return (iter != other.iter);
}